<template>
    <ul class="list">
        <li class="item"  
        v-for = "item of letters" 
        :key="item" 
        :ref = "item"
        @touchstart = "handleTouchstart"
        @touchmove = "handleTouchmove"
        @touchend = "handleTouchend" 
        @click = "handleLetterClick" 
        >{{item}}</li>
        
    </ul>
</template>

<script>
export default {
    props: {
        cities: Object,
    },
    name: 'CityAlphabet',
    data () {
        return {
            touchStatus: false
        }
    },
    computed: {
        letters () {
            const letters = []
            for(let i in this.cities){
                letters.push(i)
            }
            return letters
        }
    },
    methods: {
        handleLetterClick: function(e){
            // console.log(e.target.innerText)
            this.$emit('change',e.target.innerText)
        },
        handleTouchstart: function () {
            this.touchStatus = true
        },
        handleTouchmove: function (e) {
            if(this.touchStatus){
                const startY = this.$refs['A'][0].offsetTop
                const touchY = e.touches[0].clientY   
                const index = Math.floor((touchY-startY)/20)
                if(index >= 0 && index < this.letters.length){
                    this.$emit('change',this.letters[index])
                }
                console.log(index)
            }
        },
        handleTouchend: function () {
            this.touchStatus = false 
        }
    }
}
</script>

<style lang="stylus" scoped>
    @import '~styles/varibles.styl'
    .list
        display: flex
        flex-direction: column
        justify-content: center
        position: absolute
        right: 0
        top: 1.5rem
        bottom: 0
        width: .4rem
        .item
            text-align: center
            line-height: .44rem
            color: $bgColor
            height: .44rem
</style>